import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2022-12-28
 * Time: 下午 22:03
 */

@WebServlet("/hello")
public class ShowRequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        StringBuilder builder = new StringBuilder();
        builder.append(req.getProtocol());
        builder.append("<br>");
        builder.append(req.getMethod());
        builder.append("<br>");
        builder.append(req.getRequestURI());
        builder.append("<br>");
        builder.append(req.getContextPath());
        builder.append("<br>");
        builder.append(req.getQueryString());
        builder.append("<br>");

        //获取请求当中所有键值对的Key值
        Enumeration<String> enumeration = req.getHeaderNames();
        while(enumeration.hasMoreElements()) {
            //获取Enumeration集合当中的每一个Key值
            String str = enumeration.nextElement();
            //getHeader获取Enumeration集合中每一个Key所对应的value值
            builder.append(str + ": " + req.getHeader(str));
            builder.append("<br>");
        }

        //添加格式类型让网页正确识别br,把它当成网页的换行进行处理
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write(builder.toString());
    }
}
